/***************************************************************************
 *   Copyright (C) 2005 by ywchen2000                         *
 *   ywchen2000@yahoo.com.cn                                                          *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include<iostream>
#include<stdlib.h>
#include"public.h"
template<class T>
class r_list
{
   private:
    node<T> *head,*newnode,*last,*currnode,*prenode;
   public:
     r_list(void);
    ~r_list(void);
node<T>*  setnode(T data2,node<T>* p=NULL);     
void      creater_list(T data2);
void      Insertlast(T p);
void      deletedata(T d);
void      print(void);
void      deleter_list(void);
void      athead(void);
T         Data(void);
bool	  empty(void);
node<T>*  Next(void);
node<T>*  Currnode(void);
};
template<class T>
r_list<T>::r_list(void):head(NULL),newnode(NULL),last(NULL),currnode(NULL),prenode(NULL)
{}
template<class T>
r_list<T>::~r_list(void)
{
      deleter_list();
}
template<class T>
node<T>* r_list<T>::setnode(T data2,node<T>* p)
{
       newnode=new node<T>(data2);
       return newnode;
}
template<class T>
void r_list<T>::creater_list(T data2)
{
          newnode=new node<T>(data2);
	  if(head==NULL)
	  {
             head=newnode;
            }
          else
	   {
	    currnode=head;
            while(currnode->nextnode()!=NULL)
	    {
		    currnode=currnode->nextnode();
	    }
	    currnode->insertafter(newnode);
	    currnode=newnode;
	   }
           last=newnode;	  
}
template<class T>
void r_list<T>::Insertlast(T p)
{
	newnode=new node<T>(p);
	if(last->nextnode()==NULL)
	{
        last->insertafter(newnode);
	}
	else
	{
           throw new error_name("there are some error in last");
	}
}
template<class T>
void r_list<T>::deletedata(T d)
{
	if(head==NULL)
	 {
           throw new error_name("no data in the r_list");
            }	 	 
	if(head->data==d)
	{
    	  head=head->nextnode();
	}
	else
	{
      currnode=head;
     while(currnode!=NULL)
     {
        if(currnode->data!=d)
	{		
	prenode=currnode;
	currnode=currnode->nextnode();
     }
	else
       {
       	prenode->deleteafter();
         currnode=prenode->nextnode();
	}	
     }
	}
}
template<class T>
void r_list<T>::print(void)
{
       currnode=head;
       if(head==NULL)
       {
	   throw new error_name("no data in r_list");
        }
      while(currnode!=NULL)
      {
  //       std::cout<<currnode->data<<std::endl;
          currnode=currnode->nextnode();
        }	 
        cout<<endl;
}
template<class T>
void r_list<T>::deleter_list(void)
{
	node<T> *temp;
        if(head==NULL)
	{
          throw new error_name("no data at r_list");
	}
       while(head!=NULL)
       {
        temp=head;
        head=head->nextnode();
	delete temp;
       }
          }
template<class T>
void r_list<T>::athead(void)
{
	if(head!=NULL)
	{
        currnode=head;
	}
	else
	{
          throw new error_name("no data at head of r_list");
	}
}
template<class T>
T r_list<T>::Data(void)
{
     if(currnode!=NULL)
     {
        return currnode->data;
     }
}
template<class T>
bool r_list<T>::empty(void)
{
    if( head==NULL)
       {
         return true;     
          }
    else
      {
        return false;
         }
}
template<class T>
node<T>* r_list<T>::Next(void)
{
       if(currnode!=NULL)
       {
       currnode=currnode->nextnode();
        return currnode;
       }
       else
       {
	  return NULL;
        }
 }
template<class T>
node<T>* r_list<T>::Currnode(void)
{
        return currnode;
}






